/**
 * 日期类型扩展, 这些工具类方法注册到了全局对象DATE_UTIL中.
 */
;(function(winObj) {

    const WIN = winObj ? winObj : window;

    const VERSION = "v1.0";

    const DATE_UTIL = {
        version: function () {
            return VERSION;
        },
        /**
         * 功能: 获取当月的第一天
         */
        beginOfMonth: function (date) {
            if (!_checkParam(date)) {
                date = new Date();
            }
            var nowYear = date.getFullYear();   //当前年
            var nowMonth = date.getMonth();     //当前月

            return new Date(nowYear, nowMonth, 1);
        },
        /**
         * 功能: 获取当月的最后一天
         */
        endOfMonth: function (date) {
            if (!_checkParam(date)) {
                date = new Date();
            }
            var nowYear = date.getFullYear();   //当前年
            var nowMonth = date.getMonth();     //当前月

            return new Date(nowYear, nowMonth + 1, 0);
        },
        /**
         * 功能: 获取当天的开始
         */
        beginOfDay: function (date) {
            if (!_checkParam(date)) {
                date = new Date();
            }
            date.setHours(0);
            date.setMinutes(0);
            date.setSeconds(0);

            return date;
        },
        /**
         * 功能: 获取当天的结束
         */
        endOfDay: function (date) {
            if (!_checkParam(date)) {
                date = new Date();
            }

            date.setHours(23);
            date.setMinutes(59);
            date.setSeconds(59);

            return date;
        },
        /**
         * 功能: 格式化日期,
         * 说明:
         * 1. 可以将该方法绑定到Date的实例方法上
         * ```
         * Date.prototype.format = function (format) {
         *    var o = {
         *        "M+": this.getMonth() + 1, // month
         *        "d+": this.getDate(),    // day
         *        "h+": this.getHours(),   // hour
         *        "m+": this.getMinutes(), // minute
         *        "s+": this.getSeconds(), // second
         *        "q+": Math.floor((this.getMonth() + 3) / 3),  // quarter
         *        "S": this.getMilliseconds() // millisecond
         *    };
         *    if (/(y+)/.test(format))
         *        format = format.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
         *    for (var k in o)
         *        if (new RegExp("(" + k + ")").test(format))
         *            format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length));
         *    return format;
         * };
         * ```
         */
        format: function (date, pattern) {
            var o = {
                "M+": date.getMonth() + 1,                 //月份
                "d+": date.getDate(),                    //日
                "h+": date.getHours(),                   //小时
                "m+": date.getMinutes(),                 //分
                "s+": date.getSeconds(),                 //秒
                "q+": Math.floor((date.getMonth() + 3) / 3), //季度
                "S": date.getMilliseconds()             //毫秒
            };
            if (/(y+)/.test(pattern)) {
                pattern = pattern.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));
            }
            for (var k in o) {
                if (new RegExp("(" + k + ")").test(pattern)) {
                    pattern = pattern.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
                }
            }

            return pattern;
        },
        /**
         * 功能: 输出两个日期间的所有日期序列.
         * @param start     开始时间, yyyy-MM-dd
         * @param end       结束时间, yyyy-MM-dd
         */
        getBetweenDateStr: function (start, end) {
            var result = [];
            var beginDay = start.split("-");
            var endDay = end.split("-");
            var diffDay = new Date();
            var dateList = new Array;
            var i = 0;
            diffDay.setDate(beginDay[2]);
            diffDay.setMonth(beginDay[1] - 1);
            diffDay.setFullYear(beginDay[0]);
            result.push(start);
            while (i == 0) {
                var countDay = diffDay.getTime() + 24 * 60 * 60 * 1000;
                diffDay.setTime(countDay);
                dateList[2] = diffDay.getDate();
                dateList[1] = diffDay.getMonth() + 1;
                dateList[0] = diffDay.getFullYear();
                if (String(dateList[1]).length == 1) {
                    dateList[1] = "0" + dateList[1]
                }
                if (String(dateList[2]).length == 1) {
                    dateList[2] = "0" + dateList[2]
                }
                result.push(dateList[0] + "-" + dateList[1] + "-" + dateList[2]);
                if (dateList[0] == endDay[0] && dateList[1] == endDay[1] && dateList[2] == endDay[2]) {
                    i = 1;
                }
            }
            return result;
        }
    };

    /**
     * 功能: 判断参数是否有效
     * @param param     被检查的参数
     * @return {boolean}    true-有效, false-无效;
     */
    var _checkParam = function (param) {
        if (typeof date === "undefined") {
            return false;
        }
        if (!param instanceof Date) {
            return false;
        }
        return true;
    }

    WIN.DATE_UTIL = DATE_UTIL;

}.call(this));